using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace StudentManagementApplication.DAO
{
    public class RoleDAO : BaseDAO
    {
        public RoleDAO()
        {
        }

        public Entities.Role CreateRoleFromReader(IDataReader reader)
        {
            Entities.Role role = new Entities.Role();
            role.RoleID = (string)reader["cRoleID"];
            role.RoleDetail = (string)reader["vRoleDetail"];
            return role;
        }

        public void InsertRole(Entities.Role role)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcInsertRole", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cRoleID", role.RoleID));
                command.Parameters.Add(new SqlParameter("@vRoleDetail", role.RoleDetail));
                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)

            }
        }

        public void UpdateRole(Entities.Role role)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcUpdateRole", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@cRoleID", role.RoleID));
                command.Parameters.Add(new SqlParameter("@vRoleDetail", role.RoleDetail));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw;
            }
        }

        public List<Entities.Role> GetAllRoles()
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetAllRoles", connection);
                command.CommandType = CommandType.StoredProcedure;

                connection.Open();
                List<Entities.Role> roles = new List<Entities.Role>();
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        roles.Add(CreateRoleFromReader(reader));
                    }
                    //else
                    //    throw;
                }
                return roles;
            }
        }

        public Entities.Role GetRoleByRoleID(string sRoleID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetRoleByRoleID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("cRoleID", sRoleID));

                connection.Open();
                Entities.Role role = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if(reader.Read())
                    {
                        role = CreateRoleFromReader(reader);
                    }
                    //else
                    //    throw;
                }
                return role;
            }
        }

        public Entities.Role GetRoleByLoginID(string sLoginID)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("prcGetRoleByLoginID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("cLoginID", sLoginID));

                connection.Open();
                Entities.Role role = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        role = CreateRoleFromReader(reader);
                    }
                    //else
                    //    throw;
                }
                return role;
            }
        }

    }
}
